package test.login;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserComp extends HttpServlet {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO 自動生成されたメソッド・スタブ
		doAttrivute(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO 自動生成されたメソッド・スタブ
		doAttrivute(req, resp);
	}

	protected void doAttrivute(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		/* 画面入力情報取得 */
		String id = req.getParameter("s_id");
		String password = req.getParameter("s_password");
		String passwordr = req.getParameter("s_passwordr");
		String user = req.getParameter("s_user");
		String adminFlg = req.getParameter("s_adminflg");
		String inputFlg = req.getParameter("inputFlg");

		/* Beansに格納 */
		User us = new User();
		us.setLoginId(id);
		us.setPassword(password);
		us.setPassword(passwordr);
		us.setName(user);
		us.setAdminFlg(adminFlg.charAt(0));

		UserController uc = new UserController();

		if (inputFlg.equals("1")) {
			String targetId = req.getParameter("targetId");
			us.setUserId(Integer.valueOf(targetId));
			uc.updateUser(us);
		} else {
			uc.insertUser(us);
		}

		req.getRequestDispatcher("/WEB-INF/userInputComp.jsp").forward(req,
				resp);
	}
}